import {AxiosDownload, AxiosGetUrl, AxiosUpload} from "./HttpService";
import {addUrlParams, isArray} from "../utils";

// @ts-ignore
const uploadFile: Function = async function (file: any, isCreateThumb: boolean = true) {
    let formData = new FormData();
    formData.append("file", file);
    isCreateThumb && formData.append("toThumb", isCreateThumb);
    const res = await AxiosUpload("/file", formData);
    return res.data;
};
// @ts-ignore
const uploadIconFile: Function = async function (file: any) {
    let formData = new FormData();
    formData.append("file", file);
    const res = await AxiosUpload("/icon", formData);
    return res.data;
}
// @ts-ignore
const getIconUrl: Function = function (fileLinkId: string) {
    return AxiosGetUrl("/icon/" + fileLinkId);
};

// @ts-ignore
const getThumbById: Function = function (fileLinkId: string) {
    return AxiosGetUrl("/file/thumb/" + fileLinkId);
}

// @ts-ignore
const getPreviewFileByFileLinkId: Function = async function (fileLinkId: string) {
    return window.location.origin + addUrlParams(AxiosGetUrl("/sf-drs/platform/file/preview"), {
        fileLinkId: fileLinkId
    })
}

const downloadFile: Function = async function (ids: string | string[]) {
    let params: any = {
        fileLinkId: ids
    };
    if (isArray(ids)) {
        params = {
            fileLinkId: ids.join(",")
        }
    }
    AxiosDownload("/sf-drs/file/download", params, () => { }, "get")
}

export {
    uploadFile,
    uploadIconFile,
    getIconUrl,
    getThumbById,
    getPreviewFileByFileLinkId,
    downloadFile
}